package com.skyworth.ui.api;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.skyworth.commen.ui.R;
import com.skyworth.util.SkyScreenParams;

/**
 * Created by lu on 2018/1/22.
 */

public class SkyLoadingViewV2 extends SkySurfaceView {

    public SkyLoadingViewV2(Context context) {
        super(context);
    }

    public SkyLoadingViewV2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SkyLoadingViewV2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private static class InnerDrawHandler implements DrawHandler {
        private static final int SPEED_OFFSET = 80;
        private SkySurfaceView skySurfaceView;
        private boolean bRunning = false;
        private int time = 0;
        private static final float DURATION = 1500 * SPEED_OFFSET / 100;

        private Bitmap bitmap = null;

        public InnerDrawHandler(SkySurfaceView skySurfaceView) {
            this.skySurfaceView = skySurfaceView;
        }

        public synchronized boolean isRunning() {
            return bRunning;
        }

//        private void clear() {
//            synchronized (this) {
//                if (this.skySurfaceView != null) {
//                    this.skySurfaceView._draw(new DrawHandler() {
//                        @Override
//                        public void doDraw(Context context, Canvas canvas) {
//                            Log.d("LOADING", "do clear");
//                            canvas.drawColor(Color.WHITE);
//                            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.SRC);
//                        }
//                    });
//                }
//            }
//        }

        public synchronized void start(SkySurfaceView skySurfaceView) {
            if (bRunning)
                return;
            Log.d("LOADING", "start");
            if (bitmap == null)
                bitmap = BitmapFactory.decodeResource(skySurfaceView.getResources(), R.drawable.ui_sdk_loading_rect);
            time = 0;
            bRunning = true;
            this.skySurfaceView.draw(this);
        }

        public synchronized void stop() {
            if (!bRunning)
                return;
            Log.d("LOADING", "stop");
            if (bitmap != null && !bitmap.isRecycled()) {
                bitmap.recycle();
                bitmap = null;
            }
            bRunning = false;
        }

        private Paint init(Canvas canvas) {
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            Paint mPaint = new Paint();
            mPaint.setAntiAlias(true);
            return mPaint;
        }

        private static class DrawItem {
            private int time_0, time_1, time_2, time_3;
            private int left;
            private int dist_1, dist_2;

            public DrawItem(int time_0, int time_1, int time_2, int time_3, int left, int dist_1, int dist_2) {
                this.time_0 = time_0 * SPEED_OFFSET / 100;
                this.time_1 = time_1 * SPEED_OFFSET / 100;
                this.time_2 = time_2 * SPEED_OFFSET / 100;
                this.time_3 = time_3 * SPEED_OFFSET / 100;
                this.left = left;
                this.dist_1 = dist_1;
                this.dist_2 = dist_2;
            }

            private Bitmap prevBitmap = null;


            private synchronized Bitmap drawBitmapPostScale(Bitmap bitmap, float scale) {
                if (prevBitmap != null && prevBitmap != bitmap && !prevBitmap.isRecycled()) {
                    prevBitmap.recycle();
                    prevBitmap = null;
                }
                Matrix matrix = new Matrix();
                matrix.postScale(scale, scale);
                int width = (int) ((float) bitmap.getWidth() * scale);
                int height = (int) ((float) bitmap.getHeight() * scale);
                if (width <= 0)
                    width = 1;
                if (height <= 0)
                    height = 1;
                prevBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
//                .createBitmap(bitmap, 0, 0, bitmap.getWidth(),
//                        bitmap.getHeight(), matrix, false);
                return prevBitmap;
            }

            public void draw(Context context, Paint mPaint, Canvas canvas, int time, Bitmap bitmap) {
                int alpha = 0;
                canvas.save();
                int _left = left;
                if (time >= time_0 && time < time_1) {
                    _left += dist_1 * (time - time_0) / (time_1 - time_0);
                    alpha = 255 * (time - time_0) / (time_1 - time_0);
                } else if (time >= time_1 && time < time_2) {
                    _left += dist_1;
                    alpha = 255;
                } else if (time >= time_2 && time < time_3) {
                    _left += dist_1 + dist_2 * (time - time_2) / (time_3 - time_2);
                    alpha = 255 - 255 * (time - time_2) / (time_3 - time_2);
                } else {
                    _left = -1;
                }
                if (_left >= 0) {
//                    mPaint.setColor(Color.WHITE);
                    mPaint.setAlpha(alpha);
                    canvas.drawBitmap(bitmap, SkyScreenParams.getInstence(context).getResolutionValue(_left), 0, mPaint);

//
//                    Matrix matrix = new Matrix();
//                    matrix.postTranslate(SkyScreenParams.getInstence(context).getResolutionValue(_left), 0);
//                    matrix.postRotate(45);
//                    Rect rect = new Rect();
//                    rect.set(0, 0, 22, 22);
//                    canvas.concat(matrix);
//                    canvas.drawRect(rect, mPaint);
//                    canvas.drawBitmap(drawBitmapPostScale(bitmap, (float) alpha / 255.00f), SkyScreenParams.getInstence(context).getResolutionValue(_left), 0, mPaint);
                }
                canvas.restore();
            }
        }

        private DrawItem drawItem1 = new DrawItem(0, 560, 760, 1200, 110, 78, 108);
        private DrawItem drawItem2 = new DrawItem(80, 560, 840, 1280, 52, 96, 105);
        private DrawItem drawItem3 = new DrawItem(160, 560, 1000, 1360, 0, 108, 100);

        @Override
        public void doDraw(Context context, Canvas canvas) {
            synchronized (this) {
                if (bRunning) {
                    Log.d("LOADING", "doDraw");
                    if (time >= DURATION)
                        time = 0;
//                    Log.d("LLL", "doDraw:" + time);
                    Paint mPaint = init(canvas);
                    drawItem1.draw(context, mPaint, canvas, time, bitmap);
                    drawItem2.draw(context, mPaint, canvas, time, bitmap);
                    drawItem3.draw(context, mPaint, canvas, time, bitmap);
//
//                    drawItem(0, 27.9f, mPaint, canvas);
//                    drawItem(27.9f, 36.2f, mPaint, canvas);
//                    drawItem(64.1f, 39.8f, mPaint, canvas);

                    time += INTERVAL;
                    if (this.skySurfaceView != null) {
                        this.skySurfaceView.draw(this, (long) INTERVAL);
                    }
                }
            }
        }

        public void onSkySurfaceviewCreate() {
            if (isRunning() && skySurfaceView != null)
                skySurfaceView.draw(this);
        }
    }

    private InnerDrawHandler drawHandler = new InnerDrawHandler(this);

    @Override
    protected void onCreate(Canvas canvas) {
        super.onCreate(canvas);
        Log.d("LOADING", "onCreate");
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        setVisibility(View.VISIBLE);
        drawHandler.onSkySurfaceviewCreate();
    }

    @Override
    protected void onDestroy(Canvas canvas) {
        Log.d("LOADING", "onDestroy");
        stopAnim();
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        super.onDestroy(canvas);
    }

    public void startAnim() {
        setVisibility(View.VISIBLE);
        drawHandler.start(this);
    }

    public void stopAnim() {
        drawHandler.stop();
        setVisibility(View.GONE);
    }

    public boolean isAnimRunning() {
        return drawHandler.isRunning();
    }
}
